/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.utils;

import com.intelligent.ispc.common.utils.ResourceBundleUtils;

import javax.swing.*;

/**
 * Created by Peter on 2016/9/18.
 */
public class AppMessageFactory {
    private static final String TITLE = "Message";
    public static final int YES_OPTION = JOptionPane.YES_OPTION;
    public static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION;
    public static final int NO_OPTION = JOptionPane.NO_OPTION;

    public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
    public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
    public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE;
    public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;

    /**
     * Show success dialog with message.
     * with INFORMATION_MESSAGE icon
     *
     * @param msg message.
     */
    public static void showSuccessDialog(String msg) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_OK")};
        JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option, option[0]);
    }

    /**
     * Show error dialog with message.
     * with ERROR_MESSAGE icon
     *
     * @param msg message.
     */
    public static void showErrorDialog(String msg) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_OK")};
        JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null, option, option[0]);
    }

    /**
     * Show warning dialog with message.
     * with WARNING_MESSAGE icon
     *
     * @param msg message.
     */
    public static void showWarningDialog(String msg) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_OK")};
        JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.OK_OPTION, JOptionPane.WARNING_MESSAGE, null, option, option[0]);
    }

    /**
     * Show dialog with title and message.
     * with INFORMATION_MESSAGE icon
     *
     * @param msg   message.
     * @param title dialog title.
     */
    public static void showDialog(String msg, String title) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_OK")};
        JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option, option[0]);
    }

    /**
     * Show confirm dialog with message.
     * with WARNING_MESSAGE icon
     *
     * @param msg message.
     * @return YES_OPTION/CANCEL_OPTION
     */
    public static int showConfirmDialog(String msg) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_YES"), ResourceBundleUtils.getString("DIALOG_BUTTON_CANCEL")};
        int n = JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, option, option[0]);
        if (n == 0) {
            return YES_OPTION;
        } else if (n == 1) {
            return CANCEL_OPTION;
        }
        return CANCEL_OPTION;
    }

    /**
     * Show confirm dialog with title and message.
     *
     * @param msg message.
     * @param messageType message icon type.
     * @return YES_OPTION/CANCEL_OPTION
     */
    public static int showConfirmDialog(String msg, int messageType) {
        String[] option = new String[]{ResourceBundleUtils.getString("DIALOG_BUTTON_YES"), ResourceBundleUtils.getString("DIALOG_BUTTON_CANCEL")};
        int n = JOptionPane.showOptionDialog(null, msg, TITLE, JOptionPane.YES_NO_OPTION, messageType, null, option, option[0]);
        if (n == 0) {
            return YES_OPTION;
        } else if (n == 1) {
            return CANCEL_OPTION;
        }
        return CANCEL_OPTION;
    }
}
